package com.xuerhua.yxt.controller.sys.fruit;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xuerhua.yxt.common.RspResult;
import com.xuerhua.yxt.common.enums.Status;
import com.xuerhua.yxt.controller.BaseController;
import com.xuerhua.yxt.model.Grid;
import com.xuerhua.yxt.model.TeacherFruits;
import com.xuerhua.yxt.model.TeacherInfo;
import com.xuerhua.yxt.page.Page;
import com.xuerhua.yxt.reqVo.FruitsQueryVO;
import com.xuerhua.yxt.service.ITeacherFruitsService;
import com.xuerhua.yxt.service.ITeacherService;

@RequestMapping("sys/teacherFruit/reg")
@Controller
public class SysTeacherFruitRegController extends BaseController {
	
	@Autowired
	private ITeacherFruitsService teacherFruitsService;
	@Autowired
	private ITeacherService teacherService;
	
	@RequestMapping(value={"","/"})
	public String Index() {
		return "sys/fruits/teacherFruitReg";
	}
	
	@RequestMapping("pageQuery")
	@ResponseBody Object pageQuery(FruitsQueryVO vo){
		try {
			Page<TeacherFruits> page = new Page<>();
			page.setPageNo(vo.getPage());
			page.setPageSize(vo.getRows());
			page.setParams(vo);
			List<TeacherFruits> list = teacherFruitsService.selectListByPage(page);
			Grid grid = new Grid();
			grid.setRows(list);
			grid.setTotal(page.getTotalRecord());
			return grid;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	@RequestMapping("addUpdate")
	@ResponseBody Object addUpdate(TeacherFruits record) {
		try {
			if(record.getTeacherId() != null) {
				TeacherInfo teacher = teacherService.selectById(record.getTeacherId());
				record.setTeacherName(teacher.getName());
			}
			if(record.getId() == null) {
				teacherFruitsService.insertSelective(record);
			}else {
				teacherFruitsService.updateByPrimaryKeySelective(record);
			}
			return new RspResult(Status.SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new RspResult(Status.FAIL);
	}
}
